﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenBlog.IDAL;
using OpenBlog.Entity;
using System.Data.SqlClient;
using System.Data;

namespace OpenBlog.SQLServerDAL
{
    public class UsersDAL : IUsersDAL
    {
        public Users GetUserById(string id)
        {
            SqlParameter para = new SqlParameter("@ID", id);
            DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConntectionString, "dbo.GetUserById", para);
            Users users = new Users();
            if (ds.Tables[0].Rows.Count > 0)
            {
                users.ID = ds.Tables[0].Rows[0]["ID"].ToString();
                users.NAME = ds.Tables[0].Rows[0]["NAME"].ToString();
                users.PASSWORD = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
                users.EMAIL = ds.Tables[0].Rows[0]["EMAIL"].ToString();
                users.SEX = ds.Tables[0].Rows[0]["SEX"].ToString();
                if (ds.Tables[0].Rows[0]["BIRTHDAY"].ToString() != string.Empty)
                    users.BIRTHDAY = Convert.ToDateTime(ds.Tables[0].Rows[0]["BIRTHDAY"]);
                users.IMAGE = ds.Tables[0].Rows[0]["IMAGE"].ToString();
                if (ds.Tables[0].Rows[0]["REGISTERDATE"].ToString() != string.Empty)
                    users.REGISTERDATE = Convert.ToDateTime(ds.Tables[0].Rows[0]["REGISTERDATE"]);
                if (ds.Tables[0].Rows[0]["LOGINDATE"].ToString() != string.Empty)
                    users.LOGINDATE = Convert.ToDateTime(ds.Tables[0].Rows[0]["LOGINDATE"]);
                users.TYPE = ds.Tables[0].Rows[0]["TYPE"].ToString();
            }
            return users;
        }

        public void CreateUser(Users user)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@ID", user.ID);
            param[1] = new SqlParameter("@NAME", user.NAME);
            param[2] = new SqlParameter("@PASSWORD", user.PASSWORD);
            param[3] = new SqlParameter("@EMAIL", user.EMAIL);
            param[4] = new SqlParameter("@SEX", user.SEX);
            param[5] = new SqlParameter("@BIRTHDAY", user.BIRTHDAY);
            param[6] = new SqlParameter("@IMAGE", user.IMAGE);
            param[7] = new SqlParameter("@REGISTERDATE", user.REGISTERDATE);
            param[8] = new SqlParameter("@LOGINDATE", user.LOGINDATE);
            param[9] = new SqlParameter("@TYPE", user.TYPE);

            SqlHelper.ExecuteNonQuery(SqlHelper.ConntectionString, "dbo.InsertUser", param);
        }

        public void UpdateUser(Users user, string id)
        {
            throw new NotImplementedException();
        }

        public void deleteUser(string id)
        {
            throw new NotImplementedException();
        }

        public List<Users> ListUser()
        {
            throw new NotImplementedException();
        }
    }
}
